package servlet;

import dao.OrderDao;
import pojo.Course;
import pojo.Order;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/buyOne")
public class BuyOneServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public BuyOneServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        ArrayList<String> orders = new ArrayList<>();

        User user = (User) session.getAttribute("user");
        Course course = (Course) session.getAttribute("course");
        String id = user.getAccount() + System.currentTimeMillis() + new String(course.getId());
        new OrderDao().insertOne(new Order(id, course.getId(), user.getAccount()));
        orders.add(id);
        session.setAttribute("orders", orders);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
